﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _15_TreeView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void 更新时间ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.toolStripStatusLabel2.Text = DateTime.Now.ToString();
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_AddNode frm = new Frm_AddNode();
            if (frm.ShowDialog()==DialogResult.OK)
            {
                //判断是否选择在根节点下添加
                if (this.treeView1.SelectedNode == null)
                {
                    this.treeView1.Nodes.Add("根节点", frm.NodeName, 0);
                }
                else
                {
                    this.treeView1.SelectedNode.Nodes.Add("子节点",frm.NodeName,0);
                }
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //删除前提示用户，是否删除
            DialogResult result = MessageBox.Show("是否删除选中的节点？", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == DialogResult.Cancel)
            {
                return;
            }

            //获取选中的节点
            TreeNode node = this.treeView1.SelectedNode;
            if (node != null)
            {
                //删除节点
                this.treeView1.Nodes.Remove(node);
                //删除节点后，清空文本框
                this.richTextBox1.Text = "";
                //拼接文件路径，删除文件
                string path = this.textBox1.Text + "\\" + node.Parent.Text + "\\" + node.Text;
                System.IO.File.Delete(path);
            }
        }

        private void 重命名ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //获取选中的节点
            TreeNode node = this.treeView1.SelectedNode;
            if (node != null)
            {
                Frm_AddNode frm = new Frm_AddNode(node.Text);
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    //拼接文件路径，重命名文件
                    string path = this.textBox1.Text + "\\" + node.Parent.Text + "\\" + node.Text;
                    string newPath = this.textBox1.Text + "\\" + node.Parent.Text + "\\"  + frm.NodeName;
                    //拷贝文件
                    System.IO.File.Copy(path, newPath);
                    //原有路径的文名删除
                    System.IO.File.Delete(path);
                    //将新的文件名移动到原有路径
                    System.IO.File.Move(path, newPath);
                    //修改节点的名称
                    node.Text = frm.NodeName;
                }
            }
        }
        /// <summary>
        /// 查找路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //获取指定路径下的所有文件夹
            string [] dirs = System.IO.Directory.GetDirectories(this.textBox1.Text);
            //清空树视图
            this.treeView1.Nodes.Clear();
            //遍历文件夹，添加到树视图中
            foreach (string dir in dirs)
            {
                //获取文件夹的名称
                string dirName = System.IO.Path.GetFileName(dir);
                //添加节点
                this.treeView1.Nodes.Add(dirName, dirName, 0);
                //获取文件夹下的所有文件夹，获取其名称
                string[] subDirs = System.IO.Directory.GetDirectories(dir);
                foreach (string subDir in subDirs)
                {
                    //获取文件夹的名称
                    string subDirName = System.IO.Path.GetFileName(subDir);
                    //添加到树视图中
                    this.treeView1.Nodes[dirName].Nodes.Add(subDirName, subDirName, 0);
                    //遍历文件夹下的TXT日志文件，然后添加到树视图中
                    string[] files = System.IO.Directory.GetFiles(subDir, "*.txt");
                    foreach (string file in files)
                    {
                        //获取文件的名称
                        string fileName = System.IO.Path.GetFileName(file);
                        //添加到树视图中
                        this.treeView1.Nodes[dirName].Nodes[subDirName].Nodes.Add(fileName, fileName, 1,1);
                    }
                }

            }
            //展开所有节点
            this.treeView1.ExpandAll();
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //当点击的是文件节点时，显示文件内容
            if (e.Node.Level == 2)
            {
                //获取文件的路径
                string path = this.textBox1.Text + "\\" + e.Node.Parent.Parent.Text + "\\" + e.Node.Parent.Text + "\\" + e.Node.Text;
                //读取文件内容
                string content = System.IO.File.ReadAllText(path);
                //读取的内容使用换行符分隔保存到数组中
                string[] lines = content.Split(new string[] { "\n","\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                //清空文本框
                this.richTextBox1.Text = "";
                //遍历数组，添加到文本框中,根据这一行的关键字包含不同的颜色
                //Debug: 黑色
                //Info: 蓝色
                //Error: 红色
                //Warn: 橙色
                //Fatal:
                foreach (string line in lines)
                {
                    this.richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
                    if (line.Contains("Debug"))
                    {
                        this.richTextBox1.SelectionColor = Color.Black;
                    }
                    else if (line.Contains("Info"))
                    {
                        this.richTextBox1.SelectionColor = Color.Blue;
                    }
                    else if (line.Contains("Error"))
                    {
                        this.richTextBox1.SelectionColor = Color.Red;
                    }
                    else if (line.Contains("Warn"))
                    {
                        this.richTextBox1.SelectionColor = Color.Orange;
                    }
                    else if (line.Contains("Fatal"))
                    {
                        this.richTextBox1.SelectionColor = Color.Purple;
                    }
                    this.richTextBox1.AppendText(line + "\n");

                }

            }
        }
    }
}
